package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.util.DateUtil;

public class TaskAdapter extends ArrayAdapter<Task> {

	List<Task> tasks;
	List<Task> disabledTasks;
	List<Task> invisibleTasks;
	
	private Task selectedTask = null;
	
	public TaskAdapter(Context context, int textViewResourceId,
			List<Task> objects) {
		super(context, textViewResourceId, objects);
		tasks = objects;
		invisibleTasks = new ArrayList<Task>();
	}
	
	public TaskAdapter(Context context, int textViewResourceId,
			List<Task> objects, List<Task> disabledTasks) {
		super(context, textViewResourceId, objects);
		tasks = objects;
		this.disabledTasks = disabledTasks;
		invisibleTasks = new ArrayList<Task>();
	}	
	
	public void setTaskVisibility(Task task, boolean visible) {
		if (visible) {
			if (invisibleTasks.contains(task)) {
				invisibleTasks.remove(task);
			}
		}
		else {
			if (!invisibleTasks.contains(task)) {
				invisibleTasks.add(task);
				if (selectedTask==task)
					selectedTask = null;
			}
		}
		this.notifyDataSetChanged();
	}
	
	public void setActiveTask(Task task) {
		this.selectedTask = task;
		this.notifyDataSetChanged();
	}


	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.task_list_item, null);
            }
            
            Task task = null;
            if (position >= 0 && position < tasks.size()) {
            	task = tasks.get(position);
            }
            

            if (task != null) {
            	boolean isCyclicTask = (task instanceof CyclicTask);
            	String taskType = task.TaskType.Name;
            	String taskInfo = task.Info;
            	String taskStatus = task.TaskStatus.Name;
            	
            	String timespan = DateUtil.toTimeDistance(task.StartTime, task.EndTime);

            	TextView taskTypeTxt = ((TextView)v.findViewById(R.id.taskType));
            	TextView taskInfoField = ((TextView)v.findViewById(R.id.taskInfo));
            	TextView taskStatusTxt = ((TextView)v.findViewById(R.id.taskStatus));
            	TextView taskDateTxt = ((TextView)v.findViewById(R.id.taskTimeSpan));
            	ImageView icon = (ImageView)v.findViewById(R.id.taskIcon);
            	icon.setVisibility(View.VISIBLE);
            	
            	taskTypeTxt.setText(taskType);
            	if (taskInfo==null || taskInfo.equals(""))
            		taskInfoField.setVisibility(View.GONE);
            	else
            		taskInfoField.setText(taskInfo);
            	
            	taskStatus = String.format("%s: %s", getContext().getString(R.string.Status), taskStatus);
            	taskStatusTxt.setText(taskStatus);
            	taskDateTxt.setText(timespan);
            	icon.setImageResource(isCyclicTask ? R.drawable.cyclic_task_icon  : R.drawable.task_icon);
            	
                if (!isEnabled(position)) {
                	int color = 0x4B000000;
                	taskTypeTxt.setTextColor(color);
                	taskInfoField.setTextColor(color);
                	taskStatusTxt.setTextColor(color);
                	taskDateTxt.setTextColor(color);
                	icon.setAlpha(50);
                }
                else {
	                if (invisibleTasks.contains(task)) {
	                	icon.setVisibility(View.INVISIBLE);
	                }
	                v.setBackgroundColor(Color.TRANSPARENT);
	                if (selectedTask == task) {
	                	v.setBackgroundColor(0xfffcc521);
	                }
                }
            }
            return v;
    }	
    
    @Override
    public boolean areAllItemsEnabled() {
        return disabledTasks==null;
    }

    @Override
    public boolean isEnabled(int position) {
    	if(disabledTasks==null)
    		return true;
    	
    	Task task = tasks.get(position);
    	if (task!=null) {
    		if (!disabledTasks.contains(task)) {
    			return true;
    		}
    	}
    	return false;
    }
    
}
